<?php

require_once __DIR__ . '/vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;
$exchange = 'Gaming';
$routerKey = 'data'; //只订阅LOL消息
$connection = new AMQPStreamConnection('127.0.0.1', 5672, 'guest', 'guest', 'test');
$channel = $connection->channel(); $channel->exchange_declare($exchange, 'direct', false, false, false);
list($queueName, ,) = $channel->queue_declare("", false, false, true, false);
$channel->queue_bind($queueName, $exchange, 'lol');

echo " 等待消息中..." .PHP_EOL;
$callback = function ($msg) {
    echo '接收到消息：',$msg->delivery_info['routing_key'], ':', $msg->body, PHP_EOL;
    sleep(1);  //模拟耗时执行
};
$channel->basic_consume($queueName, '', false, true, false, false, $callback);

while ($channel->is_consuming()) {
    $channel->wait();
}

$channel->close();
$connection->close();